package com.coder520.controller;

import com.coder520.common.ServerResponse;
import com.coder520.pojo.Permission;
import com.coder520.pojo.Role;
import com.coder520.pojo.User;
import com.coder520.service.IPermissionService;
import com.coder520.service.IUserService;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Required;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * Created by ASUS on 2017/7/14.
 */
@Controller
@RequestMapping("/permission")
//@RequiresRoles({"leader"})
public class PermissionController {

    @Autowired
    private IPermissionService iPermissionService;

    @Autowired
    private IUserService iUserService;


    @RequestMapping("/perAll")
    @RequiresRoles({"leader"})
    public String permissionAll(Model model){
        List<Permission> permissionList = iPermissionService.findPerAll();
        model.addAttribute("permissions",permissionList);
        return "permissionAll";
    }

    /**
     * 查询所有用户
     * @return
     */
    @RequestMapping("/get_user_all")
    @RequiresRoles({"leader"})
    public String getUserAll(Model model){
        List<User> userList = iUserService.findUserAll(null);
        if(userList!=null){
            model.addAttribute("user_all",userList);
        }
        return "user";
    }



    @RequestMapping("/get_user_roleid")
    @ResponseBody
    @RequiresRoles({"leader"})
    public ServerResponse getUserRoleid(Long roleid){
        List<User> userList = iUserService.findUserAll(roleid);
        if(userList!=null){
            return ServerResponse.createBySuccess(userList);
        }
        return ServerResponse.createBySuccess();
    }


    /**
     * 查看所有角色
     * @param model
     * @return
     */
    @RequestMapping("/home")
    @RequiresRoles({"leader"})
    public String home(Model model){
        List<Role> roleList = iPermissionService.findAll();

        model.addAttribute("roles",roleList);
        return "permission";
    }


    /**
     * 更新或添加权限
     * @param per_id
     * @param description
     * @param permission
     * @return
     * @throws Exception
     */
    @RequestMapping("/updateOrAdd")
    @RequiresRoles({"leader"})
    public String update(Long per_id,String description,String permission) throws Exception {
        if (per_id==null){
            iPermissionService.addPer(description, permission);
            return "redirect:/permission/perAll";
        }
        iPermissionService.update(per_id, description, permission);
        return "redirect:/permission/home";
    }

    /**
     * 查询角色所有权限
     * @param
     * @return
     */
    @RequestMapping("/get_role_permission")
    @RequiresRoles({"leader"})
    @ResponseBody
    public ServerResponse getRolePermission(Long roleid){
        return iPermissionService.findPermissionByRoleId(roleid);

    }

    @RequestMapping("/updateRole")
    @RequiresRoles({"leader"})
    public String updateRole(String[] permissionid,Long roleid,String description){

        if(roleid!=null){
            iPermissionService.updateRole(roleid,permissionid,description);
        }else {
            iPermissionService.addRole(permissionid,description);
        }
        return "redirect:/permission/home";
    }


    /**
     * 查询所有权限
     * @param
     * @return
     */

    @RequestMapping("/getAll")
    @ResponseBody
    @RequiresRoles({"leader"})
    public ServerResponse getPermission(){
        List<Permission> permissionList = iPermissionService.findPerAll();
        return ServerResponse.createBySuccess(permissionList);
    }

    /**
     * 查询所有角色
     * @param
     * @return
     */

    @RequestMapping("/get_role_all")
    @ResponseBody
    @RequiresRoles({"leader"})
    public ServerResponse getRole(){
        return iPermissionService.findRoleAll();
    }






    @RequestMapping("/delete")
    public String del(Long id){
        iPermissionService.del(id);
        return "redirect:/permission/home";
    }


}
